<template>
<div>
  <div v-if="new_user_gifts.id > 0">
    <view v-if="canIUse">
      <div v-for="(item,index) in new_user_gifts.gifts" :key="index" class="gift">
        {{item.name}}
      </div>
      <button class='bottom' open-type="getUserInfo" lang="zh_CN" @getuserinfo="bindGetUserInfo">立即领取</button>
    </view>
    <view v-if="!canIUse">请升级微信版本后领取</view>
  </div>
  <div v-else style="font-size:15px;text-align:center;margin-top:20px;">活动未开启</div>
</div>
</template>

<script>
import {
  mapState,
  mapActions,
  mapGetters
} from 'vuex'
import api from '@/utils/api'

export default {
  data() {
    return {
      canIUse: wx.canIUse('button.open-type.getUserInfo'),
      invitation_user_id:0,
      new_user_gifts:{}
    }
  },
  computed: {
    ...mapGetters([
      'login_info',
      'user',
    ])
  },
  async onLoad(options) {
    this.invitation_user_id = options.invitation_user_id
    let result = await api.getNewUserGifts({brand_id:this.login_info.brand_id,status:'enable'})
    console.log('result=>',result)
    if(result.code == 0){
      this.new_user_gifts = result.data
    }
  },

  methods: {
    bindGetUserInfo(e) {
      let self = this
      /*if (self.user.id > 0 && self.user.sub_pay_count > 0) {
        wx.showToast({title: "您已是老用户",icon: "none"});
        setTimeout(function () {
          wx.switchTab({url: 'index'})
        }, 2000)
        return
      }*/
      if (e.target.errMsg == 'getUserInfo:fail auth deny') {
        wx.showToast({icon:'none',title:'请授权后再领取'})
        return
      }
      api.wechatDecryptData({
        iv: e.target.iv,
        session_key: self.login_info.session_key,
        encrypted_data: e.target.encryptedData
      }).then(result => {
        console.log('result=>',result)
        if (result.code === 0) {
          let data = {
            brand_id: self.login_info.brand_id,
            open_id: self.login_info.openid,
            union_id: result.data.unionid,
            name:result.data.nick_name,
            avatar_url:result.data.avatar_url,
            invitation_user_id:self.invitation_user_id
          }
          api.createWxUserV1(data).then(result => {
            if (result.code === 0) {
              self.$store.dispatch("setUser", result.data)
              //领取新人礼包
              api.receiveNewUserGifts({user_id:result.data.id}).then(res=>{
                if (res.code == 0) {
                  wx.showToast({title: "领取成功！",icon: "none"});
                  setTimeout(function () {
                    wx.switchTab({url: 'index'})
                  }, 2000)
                }
              })
            } else {
              console.log('createWxUser err:')
            }
          })
        }
      })
    }
  }
}
</script>

<style >
page{
  background: #EC7676;
  color: #fff;
}
.bottom {
  border-radius: 80rpx;
  margin: 70rpx 50rpx;
  font-size: 35rpx;
  background: #F01E1E;
  color: #fff;
}
.gift{
  background: #F01E1E;
  width: 90%;
  height: 60px;
  line-height: 60px;
  text-align: center;
  margin: 10px auto 0px auto;
  border-radius: 10px;
  font-size: 16px;
}
</style>
